<?php 
/**
*  * 系统-受国家计算机软件著作权保护 - !
* =========================================================
* Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: http://www.ohyu.cn
* 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
* =========================================================
* User: ohyueo
* Date: 2024/04/08
* Time: 23:34
*/
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use app\admin\traits\AdminAuth;

use app\admin\models\EscortPayRefund;
use app\admin\models\EscortPayIncome;
use PHPExcel_IOFactory;
use PHPExcel;
use app\admin\models\EscortPayPayorder;
use app\admin\models\EscortUserIntegral;
use app\admin\models\EscortUserList;
use app\admin\models\EscortUserMoneylog;

class Pay extends Common{
	use AdminAuth;

	//用户资金记录
	public function usermoneylog(){
		$dao=input('param.dao');
	   if(request()->isPost() || $dao==1){
			$page=input('param.page');
			if(!$page){
				$page=1;
			}
			$limit=input('param.limit');
			if(!$limit){
				$limit=10;//每页显示条数
			}
			$where=[];
			$id=input('param.id');
			if($id){
				$where[]=['id','=',$id];
			}
			$orderid=input('param.orderid');
			if($orderid){
				$where[]=['orderid','like','%'.$orderid.'%'];
			}
			$money=input('param.money');
			if($money){
				$where[]=['money','like','%'.$money.'%'];
			}
			$paymo=input('param.paymo');
			if($paymo){
				$where[]=['paymo','like','%'.$paymo.'%'];
			}
			$status=input('param.status');
			if($status){
				$where[]=['status','like','%'.$status.'%'];
			}
			$ordernum=input('param.ordernum');
			if($ordernum){
				$where[]=['ordernum','like','%'.$ordernum.'%'];
			}
			$trade_no=input('param.trade_no');
			if($trade_no){
				$where[]=['trade_no','like','%'.$trade_no.'%'];
			}
			$uid=input('param.uid');
			if($uid){
				$where[]=['user_id','=',$uid];
			}
			$str=input('param.str');
			$end=input('param.end');
			if($str && $end){
				$where[]=['addtime','between',[$str,$end]];
			}
			$count = EscortUserMoneylog::where($where)->count();
			$data=EscortUserMoneylog::where($where)->page($page,$limit)->order('id desc')->select()->toArray();
			$res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
			return json($res);
	   	}
		$uid=input('param.uid');
		View::assign('uid', $uid);
		//增加
		//$zjmo=Db::name('install_user_moneylog')->where('mo_type','=',1)->sum('money');
		$zjmo=EscortUserMoneylog::sumData(['where'=>['mo_type'=>1],'field'=>'money']);
		//减少
		//$jsmo=Db::name('install_user_moneylog')->where('mo_type','=',2)->sum('money');
		$jsmo=EscortUserMoneylog::sumData(['where'=>['mo_type'=>2],'field'=>'money']);
		View::assign('zjmo', $zjmo);
		View::assign('jsmo', $jsmo);

		$permis = $this->getPermissions('Pay/usermoneylog');
		View::assign('data', $this->actions);
		View::assign('permis', $permis);
		return View::fetch();
   	}
	//删除用户资金记录
	public function delmoneylog(){
		$data = array('status' => 0,'msg' => '未知错误');
		$array=input("param.id");
		if(!$array){
			$data['msg']='参数错误';return json($data);exit;
		}
		$arr=explode(",",$array);
		for($i=0;$i<count($arr);$i++){
			if($arr[$i]){
				EscortUserMoneylog::where('id',$arr[$i])->delete();
				$text = '删除了用户资金记录id='.$arr[$i].'的数据';
				$this->writeActionLog($text);
			}
		}
		$data['status']=1;
		return json($data);
	}

	//支付记录
 	public function payorder(){
 		$dao=input('param.dao');
		if(request()->isPost() || $dao==1){
			$page=input('param.page');
			if(!$page){
				$page=1;
			}
			$limit=input('param.limit');
			if(!$limit){
				$limit=10;//每页显示条数
			}
			$where=[];
			$id=input('param.id');
			if($id){
				$where[]=['id','=',$id];
			}
			$orderid=input('param.orderid');
			if($orderid){
				$where[]=['orderid','like','%'.$orderid.'%'];
			}
			$money=input('param.money');
			if($money){
				$where[]=['money','like','%'.$money.'%'];
			}
			$paymo=input('param.paymo');
			if($paymo){
				$where[]=['paymo','like','%'.$paymo.'%'];
			}
			$status=input('param.status');
			if($status){
				$where[]=['status','like','%'.$status.'%'];
			}
			$ordernum=input('param.ordernum');
			if($ordernum){
				$where[]=['ordernum','like','%'.$ordernum.'%'];
			}
			$trade_no=input('param.trade_no');
			if($trade_no){
				$where[]=['trade_no','like','%'.$trade_no.'%'];
			}
			$uid=input('param.uid');
			if($uid){
				$where[]=['uid','like','%'.$uid.'%'];
			}
			$str=input('param.str');
			$end=input('param.end');
			if($str && $end){
				$where[]=['addtime','between',[$str,$end]];
			}
			
			$count = EscortPayPayorder::where($where)->count();
			$data=EscortPayPayorder::where($where)->page($page,$limit)->order('id desc')->select()->toArray();
			$res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
			return json($res);
		}
		$permis = $this->getPermissions('pay/payorder');
		View::assign('data', $this->actions);
		View::assign('permis', $permis);
		return View::fetch();
	}
	public function addpayorder(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$orderid=input('param.orderid');
			$money=input('param.money');
			$paymo=input('param.paymo');
			$addtime=input('param.addtime');
			$status=input('param.status');
			$ordernum=input('param.ordernum');
			$trade_no=input('param.trade_no');
			$uid=input('param.uid');
			$paytime=input('param.paytime');
			$arr=array(
				'orderid'=>$orderid,
				'money'=>$money,
				'paymo'=>$paymo,
				'addtime'=>$addtime,
				'status'=>$status,
				'ordernum'=>$ordernum,
				'trade_no'=>$trade_no,
				'uid'=>$uid,
				'paytime'=>$paytime,
			);
			$id=EscortPayPayorder::insertGetId($arr);
			$text = '添加了支付记录-id='.$id;
			$this->writeActionLog($text);
			$data['status'] = 1;
			return json($data);exit;
		}
		return View::fetch('editpayorder');
	}
	public function editpayorder(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$orderid=input('param.orderid');
			$money=input('param.money');
			$paymo=input('param.paymo');
			$addtime=input('param.addtime');
			$status=input('param.status');
			$ordernum=input('param.ordernum');
			$trade_no=input('param.trade_no');
			$uid=input('param.uid');
			$paytime=input('param.paytime');
			$arr=array(
				'orderid'=>$orderid,
				'money'=>$money,
				'paymo'=>$paymo,
				'addtime'=>$addtime,
				'status'=>$status,
				'ordernum'=>$ordernum,
				'trade_no'=>$trade_no,
				'uid'=>$uid,
				'paytime'=>$paytime,
			);
			$id = input('param.id');
			if ($id){
				$id=EscortPayPayorder::where('id',$id)->update($arr);
				$text = '修改了支付记录-id='.$id;
				$this->writeActionLog($text);
				$data['status'] = 1;
				return json($data);exit;
			}
		}
		return View::fetch('editpayorder');
	}
	public function delpayorder(){
 		$data = array('status' => 0,'msg' => '未知错误');
		$array=input("param.id");
		if(!$array){
			$data['msg']='参数错误';return json($data);exit;
		}
		$arr=explode(",",$array);
		for($i=0;$i<count($arr);$i++){
			if($arr[$i]){
				//根据模型里面的baseModel方法删除数据
				
				EscortPayPayorder::where('id',$arr[$i])->delete();
				$text = '删除了支付记录id='.$arr[$i].'的数据';
				$this->writeActionLog($text);
			}
		}
		$data['status']=1;
		return json($data);
	}

	public function income(){
 		$dao=input('param.dao');
		if(request()->isPost() || $dao==1){
			$page=input('param.page');
			if(!$page){
				$page=1;
			}
			$limit=input('param.limit');
			if(!$limit){
				$limit=10;//每页显示条数
			}
			$where=[];
			$id=input('param.id');
			if($id){
				$where[]=['id','=',$id];
			}
			$title=input('param.title');
			if($title){
				$where[]=['title','like','%'.$title.'%'];
			}
			$paymo=input('param.paymo');
			if($paymo){
				$where[]=['paymo','like','%'.$paymo.'%'];
			}
			$orderid=input('param.orderid');
			if($orderid){
				$where[]=['orderid','like','%'.$orderid.'%'];
			}
			$uid=input('param.uid');
			if($uid){
				$where[]=['uid','like','%'.$uid.'%'];
			}
			$status=input('param.status');
			if($status){
				$where[]=['status','like','%'.$status.'%'];
			}
			$tuimo=input('param.tuimo');
			if($tuimo){
				$where[]=['tuimo','like','%'.$tuimo.'%'];
			}
			$usertype=input('param.usertype');
			if($usertype){
				$where[]=['usertype','like','%'.$usertype.'%'];
			}
			$tuitime=input('param.tuitime');
			if($tuitime){
				$where[]=['tuitime','like','%'.$tuitime.'%'];
			}
			$merid=input('param.merid');
			if($merid){
				$where[]=['merid','like','%'.$merid.'%'];
			}
			$str=input('param.str');
			$end=input('param.end');
			if($str && $end){
				$where[]=['addtime','between',[$str,$end]];
			}
			//根据模型里面的baseModel方法获取数据
			$count = EscortPayIncome::countData(['where'=>$where]);
			//$count = EscortPayIncome::where($where)->count();
			if($dao!=1){
				$data=EscortPayIncome::findAllData(['where'=>$where,'limit'=>$limit,'page'=>$page,'order'=>'id Desc','field'=>'*']);
				//$data=EscortPayIncome::where($where)->page($page,$limit)->order('id desc')->select()->toArray();
			}
			if($dao==1){
				$data=EscortPayIncome::findAllData(['where'=>$where,'order'=>'id Desc','field'=>'*']);
				//$data=EscortPayIncome::where($where)->order('id desc')->select()->toArray();
			}
			$res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
			return json($res);
		}
		$permis = $this->getPermissions('pay/income');
		View::assign('data', $this->actions);
		View::assign('permis', $permis);
		return View::fetch();
	}
	public function addincome(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$title=input('param.title');
			$paymo=input('param.paymo');
			$orderid=input('param.orderid');
			$uid=input('param.uid');
			$addtime=input('param.addtime');
			$status=input('param.status');
			$tuimo=input('param.tuimo');
			$usertype=input('param.usertype');
			$tuitime=input('param.tuitime');
			$merid=input('param.merid');
			$arr=array(
				'title'=>$title,
				'paymo'=>$paymo,
				'orderid'=>$orderid,
				'uid'=>$uid,
				'addtime'=>$addtime,
				'status'=>$status,
				'tuimo'=>$tuimo,
				'usertype'=>$usertype,
				'tuitime'=>$tuitime,
				'merid'=>$merid,
			);
			//$id=EscortPayIncome::insertGetId($arr);
			//根据模型里面的baseModel方法添加数据
			$id=EscortPayIncome::addData($arr);
			$id=$id->id;
			$text = '添加了收入记录-id='.$id;
			$this->writeActionLog($text);
			$data['status'] = 1;
			return json($data);exit;
		}
		return View::fetch('editincome');
	}
	public function editincome(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$title=input('param.title');
			$paymo=input('param.paymo');
			$orderid=input('param.orderid');
			$uid=input('param.uid');
			$addtime=input('param.addtime');
			$status=input('param.status');
			$tuimo=input('param.tuimo');
			$usertype=input('param.usertype');
			$tuitime=input('param.tuitime');
			$merid=input('param.merid');
			$arr=array(
				'title'=>$title,
				'paymo'=>$paymo,
				'orderid'=>$orderid,
				'uid'=>$uid,
				'addtime'=>$addtime,
				'status'=>$status,
				'tuimo'=>$tuimo,
				'usertype'=>$usertype,
				'tuitime'=>$tuitime,
				'merid'=>$merid,
			);
			$id = input('param.id');
			if ($id){
				//根据模型里面的baseModel方法修改数据
				EscortPayIncome::updatesData(['where'=>['id'=>$id],'data'=>$arr]);
				//$id=EscortPayIncome::where('id',$id)->update($arr);
				$text = '修改了收入记录-id='.$id;
				$this->writeActionLog($text);
				$data['status'] = 1;
				return json($data);exit;
			}
		}
		return View::fetch('editincome');
	}
	public function delincome(){
 		$data = array('status' => 0,'msg' => '未知错误');
		$array=input("param.id");
		if(!$array){
			$data['msg']='参数错误';return json($data);exit;
		}
		$arr=explode(",",$array);
		for($i=0;$i<count($arr);$i++){
			if($arr[$i]){
				//根据模型里面的baseModel方法删除数据
				EscortPayIncome::deleteData(['where'=>['id'=>$arr[$i]]]);
				//EscortPayIncome::where('id',$arr[$i])->delete();
				$text = '删除了收入记录id='.$arr[$i].'的数据';
				$this->writeActionLog($text);
			}
		}
		$data['status']=1;
		return json($data);
	}
	public function refund(){
 		$dao=input('param.dao');
		if(request()->isPost() || $dao==1){
			$page=input('param.page');
			if(!$page){
				$page=1;
			}
			$limit=input('param.limit');
			if(!$limit){
				$limit=10;//每页显示条数
			}
			$where=[];
			$id=input('param.id');
			if($id){
				$where[]=['id','=',$id];
			}
			$title=input('param.title');
			if($title){
				$where[]=['title','like','%'.$title.'%'];
			}
			$orderid=input('param.orderid');
			if($orderid){
				$where[]=['orderid','like','%'.$orderid.'%'];
			}
			$uid=input('param.uid');
			if($uid){
				$where[]=['uid','like','%'.$uid.'%'];
			}
			$paytime=input('param.paytime');
			if($paytime){
				$where[]=['paytime','like','%'.$paytime.'%'];
			}
			$tuitime=input('param.tuitime');
			if($tuitime){
				$where[]=['tuitime','like','%'.$tuitime.'%'];
			}
			$tuimsg=input('param.tuimsg');
			if($tuimsg){
				$where[]=['tuimsg','like','%'.$tuimsg.'%'];
			}
			$usertype=input('param.usertype');
			if($usertype){
				$where[]=['usertype','like','%'.$usertype.'%'];
			}
			$userid=input('param.userid');
			if($userid){
				$where[]=['userid','=',$userid];
			}
			$payid=input('param.payid');
			if($payid){
				$where[]=['payid','like','%'.$payid.'%'];
			}
			$tuimo=input('param.tuimo');
			if($tuimo){
				$where[]=['tuimo','like','%'.$tuimo.'%'];
			}
			$paymo=input('param.paymo');
			if($paymo){
				$where[]=['paymo','like','%'.$paymo.'%'];
			}
			$money=input('param.money');
			if($money){
				$where[]=['money','like','%'.$money.'%'];
			}
			$merid=input('param.merid');
			if($merid){
				$where[]=['merid','like','%'.$merid.'%'];
			}
			$str=input('param.str');
			$end=input('param.end');
			if($str && $end){
				$where[]=['addtime','between',[$str,$end]];
			}
			//根据模型里面的baseModel方法获取数据
			$count = EscortPayRefund::countData(['where'=>$where]);
			//$count = EscortPayRefund::where($where)->count();
			if($dao!=1){
				//$data=EscortPayRefund::where($where)->page($page,$limit)->order('id desc')->select()->toArray();
				$data=EscortPayRefund::findAllData(['where'=>$where,'limit'=>$limit,'page'=>$page,'order'=>'id Desc','field'=>'*']);
			}
			if($dao==1){
				//$data=EscortPayRefund::where($where)->order('id desc')->select()->toArray();
				$data=EscortPayRefund::findAllData(['where'=>$where,'order'=>'id Desc','field'=>'*']);
			}
			$res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
			return json($res);
		}
		$permis = $this->getPermissions('pay/refund');
		View::assign('data', $this->actions);
		View::assign('permis', $permis);
		return View::fetch();
	}
	public function addrefund(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$title=input('param.title');
			$orderid=input('param.orderid');
			$uid=input('param.uid');
			$paytime=input('param.paytime');
			$tuitime=input('param.tuitime');
			$addtime=input('param.addtime');
			$tuimsg=input('param.tuimsg');
			$usertype=input('param.usertype');
			$userid=input('param.userid');
			$payid=input('param.payid');
			$tuimo=input('param.tuimo');
			$paymo=input('param.paymo');
			$money=input('param.money');
			$merid=input('param.merid');
			$arr=array(
				'title'=>$title,
				'orderid'=>$orderid,
				'uid'=>$uid,
				'paytime'=>$paytime,
				'tuitime'=>$tuitime,
				'addtime'=>$addtime,
				'tuimsg'=>$tuimsg,
				'usertype'=>$usertype,
				'userid'=>$userid,
				'payid'=>$payid,
				'tuimo'=>$tuimo,
				'paymo'=>$paymo,
				'money'=>$money,
				'merid'=>$merid,
			);
			//根据模型里面的baseModel方法添加数据
			$id=EscortPayRefund::addData($arr);
			$id=$id->id;
			//$id=EscortPayRefund::insertGetId($arr);
			$text = '添加了退款记录-id='.$id;
			$this->writeActionLog($text);
			$data['status'] = 1;
			return json($data);exit;
		}
		return View::fetch('editrefund');
	}
	public function editrefund(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$title=input('param.title');
			$orderid=input('param.orderid');
			$uid=input('param.uid');
			$paytime=input('param.paytime');
			$tuitime=input('param.tuitime');
			$addtime=input('param.addtime');
			$tuimsg=input('param.tuimsg');
			$usertype=input('param.usertype');
			$userid=input('param.userid');
			$payid=input('param.payid');
			$tuimo=input('param.tuimo');
			$paymo=input('param.paymo');
			$money=input('param.money');
			$merid=input('param.merid');
			$arr=array(
				'title'=>$title,
				'orderid'=>$orderid,
				'uid'=>$uid,
				'paytime'=>$paytime,
				'tuitime'=>$tuitime,
				'addtime'=>$addtime,
				'tuimsg'=>$tuimsg,
				'usertype'=>$usertype,
				'userid'=>$userid,
				'payid'=>$payid,
				'tuimo'=>$tuimo,
				'paymo'=>$paymo,
				'money'=>$money,
				'merid'=>$merid,
			);
			$id = input('param.id');
			if ($id){
				//根据模型里面的baseModel方法修改数据
				EscortPayRefund::updateData(['where'=>['id'=>$id]],$arr);
				//$id=EscortPayRefund::where('id',$id)->update($arr);
				$text = '修改了退款记录-id='.$id;
				$this->writeActionLog($text);
				$data['status'] = 1;
				return json($data);exit;
			}
		}
		return View::fetch('editrefund');
	}
	public function delrefund(){
 		$data = array('status' => 0,'msg' => '未知错误');
		$array=input("param.id");
		if(!$array){
			$data['msg']='参数错误';return json($data);exit;
		}
		$arr=explode(",",$array);
		for($i=0;$i<count($arr);$i++){
			if($arr[$i]){
				//根据模型里面的baseModel方法删除数据
				EscortPayRefund::deleteData(['where'=>['id'=>$arr[$i]]]);
				//EscortPayRefund::where('id',$arr[$i])->delete();
				$text = '删除了退款记录id='.$arr[$i].'的数据';
				$this->writeActionLog($text);
			}
		}
		$data['status']=1;
		return json($data);
	}
	public function integrallog(){
		if(request()->isPost()){
			$page=input("param.page");
			if(!$page){
				$page=1;
			}
			$where=[];
			$uid=input("param.uid");
			if($uid){
				$where[]=['user_id','=',$uid];
			}
			$nick=input("param.nick");
			if($nick){
				$where[]=['u.nick','like','%'.$nick.'%'];
			}
			$limit=input("param.limit");
			if(!$limit){
				$limit=10;//每页显示条数
			}
			$str=input('param.str');
			$end=input('param.end');
			if($str && $end){
				$where[]=['addtime','between',[$str,$end]];
			}
			$count=EscortUserIntegral::countData(['where'=>$where]);
			$data=EscortUserIntegral::findAllData(['where'=>$where,'limit'=>$limit,'page'=>$page,'order'=>'id Desc','field'=>'*']);
			if($data){
				foreach($data as $k=>$v){
					$uid=$v['user_id'];
					$nick=EscortUserList::valueData(['where'=>['id'=>$uid],'field'=>'nick']);
					$data[$k]['nick']=$nick;
				}
			}
			$res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
			return json($res);
		}
        $uid=input("param.uid");
        View::assign('uid', $uid);

        $permis = $this->getPermissions('Pay/integrallog');
        View::assign('data', $this->actions);
        View::assign('permis', $permis);
        //增加
        //$zjmo=Db::name('user_integral')->where('mo_type','=',1)->sum('money');
		$zjmo=EscortUserIntegral::sumData(['where'=>['mo_type'=>1],'field'=>'money']);
        //减少
        //$jsmo=Db::name('user_integral')->where('mo_type','=',2)->sum('money');
		$jsmo=EscortUserIntegral::sumData(['where'=>['mo_type'=>2],'field'=>'money']);
        View::assign('zjmo', $zjmo);
        View::assign('jsmo', $jsmo);
        return View::fetch();
    }
	public function delintegrallog(){
        $data = array('status' => 0,'msg' => '未知错误');
        $array=input("param.id");
        if(!$array){
            $data['msg']='参数错误';return json($data);exit;
        }
        $arr=explode(",",$array);
        for($i=0;$i<count($arr);$i++){
            //删除数据
            if($arr[$i]){
                //Db::name('user_integral')->where('id',$arr[$i])->delete();
				EscortUserIntegral::deleteData(['where'=>['id'=>$arr[$i]]]);
                $text = '删除了积分记录id='.$arr[$i].'的数据';
                $this->writeActionLog($text);
            }
        }
        $data['status']=1;
        return json($data);
    }
} 